﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using CSF.Utility.Mvc;
using CSF.Service.Accounts.Client;
using CSF.Security.Data;
using CSF.Utility.Misc;
using CSF.Customers.Data;

namespace CSF.WebApp.Admin.Areas.Customers.Controllers
{
    public class CustomerController : SecureController
    {
        /// <summary>
        /// author:andy-peng
        /// date:2011-08-23
        /// 客户列表
        /// </summary>
        /// <param name="isDisable"></param>
        /// <returns></returns>
        [CsfAuthorize]
        public ActionResult Index(bool isDisable = false)
        {
            CustomerSvcClient svc = new CustomerSvcClient();
            IEnumerable<Customer> list = null;
            if (isDisable)
            {
                list = svc.GetDisable().Where(p => p.ParentId == null);
            }
            else
            {
                list = svc.GetEnable().Where(p => p.ParentId == null);
            }
            list = Search(list);
            return View(list);
        }
        /// <summary>
        /// author:andy-peng
        /// date:2011-08-23
        /// 客户列表搜索
        /// </summary>
        /// <param name="isDisable"></param>
        /// <returns></returns>
        [NonAction]
        private IEnumerable<Customer> Search(IEnumerable<Customer> list)
        {
            var collection = Request.QueryString;
            var Code = collection["Code"];
            var name = collection["Name1"];
            if (!string.IsNullOrWhiteSpace(Code))
            {
                list = list.Where(p => Code.InStr(p.Code));
            }
            if (!string.IsNullOrWhiteSpace(name))
            {
                list = list.Where(p => name.InStr(new string[] { p.NickName, p.FullName }));
            }
            return list;
        }
        [CsfAuthorize]
        public ActionResult Add(int ParentId = 0)
        {
            CountrySvcClient svcc = new CountrySvcClient();
            CustomerTypeSvcClient svcct = new CustomerTypeSvcClient();
            RegionSvcClient srcr = new RegionSvcClient();
            CreditClassSvcClient svccredit = new CreditClassSvcClient();
            ViewData["Country"] = new SelectList(svcc.GetEnable(), "Code", "Name1");
            ViewData["CustomerType"] = new SelectList(svcct.GetEnable(), "Id", "Name1");
            ViewData["Region"] = new SelectList(srcr.GetEnable(), "Code", "Name1");
            ViewData["CreditClass"] = new SelectList(svccredit.GetEnable(), "Code", "Name1");

            Customer model = new Customer();
            CustomerSvcClient svccus = new CustomerSvcClient();
            if (ParentId > 0)
            {
                var cus = svccus.GetOneById(ParentId);
                model.ParentId = ParentId;
                model.CustomerTypeId = cus.CustomerTypeId;
                model.CountryCode = cus.CountryCode;
                model.RegionCode = cus.RegionCode;
                model.CreditClassCode = cus.CreditClassCode;
                return View(model);
            }
            return View();
        }
        /// <summary>
        /// author:andy-peng
        /// date:2011-08-23
        /// 新增客户
        /// </summary>
        /// <param name="isDisable"></param>
        /// <returns></returns>
        [CsfAuthorize]
        [HttpPost]
        public ActionResult Add(Customer model, IEnumerable<int> checkedall)
        {
            if (ModelState.IsValid)
            {
                model.CreateBy = User.Identity.Name;
                CustomerSvcClient svc = new CustomerSvcClient();
                int roleid = svc.Insert(model);
                return RedirectToAction("Index");
            }
            return View(model);
        }
        /// <summary>
        /// author:andy-peng
        /// date:2011-08-23
        /// 编辑客户
        /// </summary>
        /// <param name="isDisable"></param>
        /// <returns></returns>
        public ActionResult Edit(int id)
        {
            CustomerSvcClient svc = new CustomerSvcClient();
            Customer model = svc.GetOneById(id);
            CountrySvcClient svcc = new CountrySvcClient();
            CustomerTypeSvcClient svcct = new CustomerTypeSvcClient();
            RegionSvcClient srcr = new RegionSvcClient();
            CreditClassSvcClient svccredit = new CreditClassSvcClient();
            ViewData["Country"] = new SelectList(svcc.GetEnable(), "Code", "Name1");
            ViewData["CustomerType"] = new SelectList(svcct.GetEnable(), "Id", "Name1");
            ViewData["Region"] = new SelectList(srcr.GetEnable(), "Code", "Name1");
            ViewData["CreditClass"] = new SelectList(svccredit.GetEnable(), "Code", "Name1");
            return View(model);
        }

        /// <summary>
        /// author:andy-peng
        /// date:2011-08-23
        /// 编辑客户
        /// </summary>
        /// <param name="isDisable"></param>
        /// <returns></returns>
        [CsfAuthorize]
        [HttpPost]
        public ActionResult Edit(Customer model, IEnumerable<int> checkedall)
        {
            if (ModelState.IsValid)
            {
                model.ModifyBy = User.Identity.Name;
                CustomerSvcClient svc = new CustomerSvcClient();
                svc.UpdateById(model, model.Id);
                return RedirectToAction("Index");
            }
            return View(model);
        }

        /// <summary>
        /// author:andy-peng
        /// date:2011-08-23
        /// 删除客户
        /// </summary>
        /// <param name="isDisable"></param>
        /// <returns></returns>
        [CsfAuthorize]
        public ActionResult Delete(IEnumerable<int> id, string toUrl)
        {
            if (id != null && id.Count() > 0)
            {
                CustomerSvcClient svc = new CustomerSvcClient();
                svc.DeleteById(id);
            }
            return Redirect(toUrl);
        }



        /// <summary>
        /// author:andy-peng
        /// date:2011-08-23
        /// 客户列表
        /// </summary>
        /// <param name="isDisable"></param>
        /// <returns></returns>
        [CsfAuthorize]
        public ActionResult SubCustomer(int ParentId = 0, bool isDisable = false)
        {
            CustomerSvcClient svc = new CustomerSvcClient();
            IEnumerable<Customer> list = null;
            if (isDisable)
            {
                if (ParentId > 0)
                {
                    list = svc.GetDisable().Where(p => p.ParentId == ParentId);
                }
                else { list = svc.GetDisable().Where(p => p.ParentId == null); }
            }
            else
            {
                if (ParentId > 0)
                {
                    list = svc.GetEnable().Where(p => p.ParentId == ParentId);
                }
                else
                {
                    list = svc.GetEnable().Where(p => p.ParentId == null);
                }
            }
            list = Search(list);
            return View(list);
        }

    }
}
